﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace studentskicentarETFa
{
    partial class DAL
    {
        public class StudentDAO : IDaoCrud<Student>
        {
            protected MySqlCommand komanda;

            public long create(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("insert into Studenti values ('" 
                        +entitet.ID_Clana1 + "', '" + entitet.ID_Kartice1 + "', '"
                        + entitet.Ime + "', '" + entitet.Prezime + "', '" + entitet.BrojIndexa.ToString() + "', '"
                       + entitet.BrojTelefona + "', '");
                    komanda.ExecuteNonQuery();
                    return komanda.LastInsertedId;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Student read(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("select * from Studenti where id_clana = '" + entitet.ID_Clana1.ToString() + "';", con);
                    MySqlDataReader citac = komanda.ExecuteReader();
                    Student student = null;
                    while (citac.Read())
                    {
                        //if (entitet.Ime == citac.GetString("ime") && entitet.Prezime == citac.GetString("prezime"))
                        student = new Student(citac.GetString("ime"), citac.GetString("prezime"));
                        return student;
                    }
                    return student;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public Student update(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("update Studenti set ime = '" + entitet.Ime +
                        "', prezime ='" + entitet.Prezime +
                        "', brojTelefona = " + entitet.BrojTelefona.ToString() +
                        ", sifra = '" + entitet.ID_Clana1 +
                        "', datumRegistracije = ToDate('" + entitet.Datum_registracije.ToString() +
                        "', DD.MM.YYYY) where id_clana = " + entitet.ID_Clana1, con);
                    komanda.ExecuteNonQuery();
                    return read(entitet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public void delete(Student entitet)
            {
                try
                {
                    komanda = new MySqlCommand("delete from Studenti where id_clana = " + entitet.ID_Clana1 + ";", con);
                    komanda.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            public List<Student> getAll()
            {
                try
                {
                    komanda = new MySqlCommand("select * from studenti", con);
                    MySqlDataReader r = komanda.ExecuteReader();
                    List<Student> studenti = new List<Student>();
                    while (r.Read())
                        studenti.Add(new Student(r.GetString("ime"), r.GetString("prezime"), r.GetString("idClana"), r.GetString("idKartice"), r.GetString("brojTel"), r.GetInt32("brojIndexa"), r.GetDateTime("datum")));
                    return studenti;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }


            public List<Student> getByExample(string ime, string prezime)
            {
                try
                {
                    MySqlCommand n = new MySqlCommand("se1ect ‘from Studenti where '" + prezime + "'='" + ime + "'", con);
                    List<Student> studenti = new List<Student>();
                    MySqlDataReader r = n.ExecuteReader();
                    while (r.Read())
                    {
                        studenti.Add(new Student(r.GetString("ime"), r.GetString("prezime")));
                    }
                    return studenti;
                }
                catch (Exception)
                {
                    throw;
                }

            }

            public Student getById(int id)
            {
                try
                {
                    MySqlCommand n = new MySqlCommand("se1ect * from Studenti where IDClana1='" + id + "", con);
                    Student s = null;
                    MySqlDataReader r = n.ExecuteReader();
                    while (r.Read())
                    {
                        s = new Student(r.GetString("Ime"),r.GetString("Prezime"));
                        return s;
                    }
                    return s;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
    }
}
    
